﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Speech.Synthesis;
using System.Configuration;
using System.Threading;
using System.IO;
using Syntezator_mowy.Extras;

namespace Syntezator_mowy.Tts
{
    public class TtsActionModule
    {
        private string jezyk;
        private SynchrBuffer<string> fileBuffer;
        private SynchrBuffer<string> dataBuffer;

        private string fileBaseName;
        private string fileExtension;

        private bool endAdding;
        private int waitingTime;

        public bool EndAddingData
        {
            set { this.endAdding = value; }
        }

        public TtsActionModule(Jezyk jezyk,
            ref SynchrBuffer<string> fileBuffer,
            ref SynchrBuffer<string> dataBuffer, 
            string baseName, string extension)
        {
            this.ustawJezyk(jezyk);

            this.fileBuffer = fileBuffer;
            this.dataBuffer = dataBuffer;

            this.fileBaseName = baseName;
            this.fileExtension = extension;

            this.endAdding = false;
            this.waitingTime = int.Parse(ConfigurationManager.AppSettings["ttsActionModule_waitingTime"].ToString());
        }

        private void ustawJezyk(Jezyk jezyk)
        {
            switch (jezyk)
            {
                case Jezyk.POLSKI:
                    this.jezyk = "pl-PL";
                    break;
                default:
                    break;
            }
        }

        public void run()
        {
            using (SpeechSynthesizer synth = new SpeechSynthesizer())
            {
                int index = 1;
                PromptBuilder pb = new PromptBuilder(new System.Globalization.CultureInfo(this.jezyk));
                while (!this.endAdding)
                {
                    string nazwaAktualnegoPliku = this.fileBaseName + "_" + index.ToString() + "." + this.fileExtension;
                    string dane = "";
                    dane = this.dataBuffer.wyciagnij();
                    
                    if (dane != default(string))
                    {
                        index = saveDataToActualFile(synth, index, pb, nazwaAktualnegoPliku, dane);
                    }
                    Thread.Sleep(this.waitingTime);
                }
                while (this.dataBuffer.zwrocIlosc() > 0)
                {
                    string nazwaAktualnegoPliku = this.fileBaseName + "_" + index.ToString() + "." + this.fileExtension;
                    string dane = this.dataBuffer.wyciagnij();
                    index = saveDataToActualFile(synth, index, pb, nazwaAktualnegoPliku, dane);
                }
            }
        }

        private int saveDataToActualFile(SpeechSynthesizer synth, int index, PromptBuilder pb, string nazwaAktualnegoPliku, string dane)
        {
            synth.SetOutputToWaveFile(nazwaAktualnegoPliku);
            pb.AppendText(dane);
            synth.Speak(pb);
            index++;
            synth.SetOutputToNull();
            this.fileBuffer.dodaj(nazwaAktualnegoPliku);
            pb.ClearContent();
            return index;
        }
    }
}
